package com.example.speeddating.mapper;

import com.example.speeddating.entity.Like;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 *
 * @author tina
 * @since 2023-06-28 11:14:33
 */
@Mapper
public interface LikeMapper extends BaseMapper<Like> {

    /**
     * 文章点赞数
     * @param articleId
     * @return
     */
    @Select(value = "SELECT count(*) FROM `slike` WHERE article_id = #{articleId} AND deleted = 0")
    public Integer selectArticleLikeNum(Long articleId);


    /**
     * 评论点赞数
     * @param commentId
     * @return
     */
    @Select(value = "SELECT count(*) FROM `slike` WHERE comment_id = #{commentId} AND deleted = 0")
    public Integer selectCommentLikeNum(Long commentId);

    /**
     * 插入点赞记录
     * @param like
     * @return
     */
    @Insert(value = "INSERT INTO `slike` ( user_id,article_id, comment_id,create_time )  VALUES  ( #{userId},#{articleId},#{commentId},NOW() )")
    public int insertLike(Like like);

    /**
     * 查询本人是否点赞评论
     */
    @Select(value = "SELECT count(*) FROM `slike` WHERE comment_id = #{commentId} AND user_id = #{curUserId} AND deleted = 0")
    public Integer selectIsLike(@Param("commentId") long commentId,@Param("curUserId") long curUserId);

    /**
     * 查询本人是否点赞文章
     * @param articleId
     * @param curUserId
     * @return
     */
    @Select(value = "SELECT count(*) FROM `slike` WHERE article_id = #{articleId} AND user_id = #{curUserId}  AND deleted = 0")
    public Integer selectIsLike2(@Param("articleId") long articleId,@Param("curUserId") long curUserId);

    /**
     * 删除点赞
     */
    @Delete(value = "DELETE FROM `slike` WHERE article_id = #{articleId} AND comment_id = #{commentId} AND user_id = #{curUserId}  AND deleted = 0")
    public int deleteLike(@Param("articleId") Object articleId,@Param("commentId") Object commentId,@Param("curUserId") long curUserId);

    /**
     * 是否在今日点赞过
     * @param userId
     * @return
     */
    @Select("SELECT count(*) FROM `slike` WHERE DATE(create_time) = CURDATE() AND user_id = #{userId}  AND deleted = 0")
    public int selectIsLikeInToday(long userId);


    /**
     * 点赞文章的
     * @param articleId
     * @return
     */
    @Select("select * from `slike` where article_id = #{articleId} and deleted = 0 ORDER BY create_time DESC")
    public List<Like> getLikesByArticleId(Long articleId);


    /**
     * 评论文章的
     * @param userId
     * @return
     */
    @Select("SELECT * FROM slike WHERE comment_id in (SELECT id FROM `comment` WHERE user_id = #{userId} and  deleted = 0) and deleted = 0 ORDER BY create_time DESC")
    public List<Like> getLikesCommentByArticleId(Long userId);

    /**
     * 点赞文章未读的
     * @param userId
     * @return
     */
    @Select("select count(*) from slike where article_id in (select id from article where user_id = #{userId} and deleted = 0) and deleted = 0 AND is_read = 0 AND user_id !=10015")
    public Integer getUnreadCountLikeByArticleId(Long userId);

    /**
     * 点赞评论未读的
     * @param userId
     * @return
     */
    @Select("SELECT count(*) FROM slike WHERE comment_id in (SELECT id FROM `comment` WHERE user_id = #{userId} and deleted = 0)AND  deleted = 0 AND is_read = 0 AND user_id !=10015")
    public Integer getUnreadCountLIkeCommentInReplayComment(Long userId);

    /**
     * 更新点赞文章未读的
     * @param userId
     * @return
     */
    @Update("update slike set is_read = 1 where article_id in (select id from article where user_id = #{userId} and deleted = 0) and deleted = 0 AND is_read = 0 AND user_id !=10015")
    public void updateUnreadLIkeInArticleLike(Long userId);

    /**
     * 更新点赞评论未读的
     * @param userId
     * @return
     */
    @Update("update slike set is_read = 1 WHERE comment_id in (SELECT id FROM `comment` WHERE user_id = #{userId} and deleted = 0)AND  deleted = 0 AND is_read = 0 AND user_id !=10015")
    public void updateUnreadLIkeInReplayComment(Long userId);

}
